package com.wanpinghui.supplier.widgets;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.Config;
import com.wanpinghui.supplier.R;
import com.yunshl.yunshllibrary.utils.SoftInputUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.view.YunShlPopupWinow;

/**
 * Created by xiongning on 17-12-4.
 */

public class IMInputView extends LinearLayout {

    private LinearLayout ll_input_area;

    private Button btn_input_switch;

    private EditText et_msg;

    private AudioRecorderButton btn_speak;

    private Button btn_add;

    private Button btn_send;

    private LinearLayout ll_attach_area;

    private RelativeLayout rl_take_photo;

    private RelativeLayout rl_photo;

    private RelativeLayout rl_doc;

    private RelativeLayout mLayoutTrade;
    private View mLineTrade;


    private boolean isShowAttach = false;

    private boolean isVoice = false;

    private IMinputListener mIMinputListener;
    private YunShlPopupWinow popupWinow;

    public interface IMinputListener {
        void onSendTxtMsg(String msg);

        void onSelectPhoto();

        void onTakePhoto();

        void onSelectDoc();

        void onSendAudio(String audioPath, int sends);

        void onTrade(boolean buy);

        void onRequestPermission();
    }

    public IMInputView(Context context) {
        super(context);
        init(context);
    }

    public IMInputView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IMInputView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public IMInputView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(final Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View mView = inflater.inflate(R.layout.view_im_input, this);
        ll_input_area = (LinearLayout) mView.findViewById(R.id.ll_input_area);
        btn_input_switch = (Button) mView.findViewById(R.id.btn_input_switch);
        et_msg = (EditText) mView.findViewById(R.id.et_msg);
        btn_send = (Button) mView.findViewById(R.id.btn_send);
        btn_speak = (AudioRecorderButton) mView.findViewById(R.id.btn_speak);
        btn_add = (Button) mView.findViewById(R.id.btn_add);
        ll_attach_area = (LinearLayout) mView.findViewById(R.id.ll_attach_area);
        rl_take_photo = (RelativeLayout) mView.findViewById(R.id.rl_take_photo);
        rl_photo = (RelativeLayout) mView.findViewById(R.id.rl_photo);
        rl_doc = (RelativeLayout) mView.findViewById(R.id.rl_doc);
        mLineTrade = mView.findViewById(R.id.view_line_trade);
        mLayoutTrade = (RelativeLayout) mView.findViewById(R.id.rl_trade);
/*
        if (Config.VERSION == 1) {
            mLayoutTrade.setVisibility(GONE);
            mLineTrade.setVisibility(GONE);
        } else {
            mLayoutTrade.setVisibility(VISIBLE);
            mLineTrade.setVisibility(VISIBLE);
        }*/

        mLayoutTrade.setVisibility(GONE);
        mLineTrade.setVisibility(GONE);

        btn_input_switch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onInputSwitch(v);
            }
        });
        btn_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                SoftInputUtil.hideSoftInput(getContext(), v);
                onAdd();
            }
        });
        btn_add.setVisibility(View.VISIBLE);
        btn_send.setVisibility(View.GONE);
        et_msg.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == KeyEvent.ACTION_DOWN || actionId == EditorInfo.IME_ACTION_DONE) {
                    //业务代码
                    String msgTxt = v.getText().toString();
                    if (TextUtil.isNotEmpty(msgTxt) && mIMinputListener != null) {
                        mIMinputListener.onSendTxtMsg(msgTxt);
                    }
                    et_msg.setText("");
                }
                return true;
            }
        });
        et_msg.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                goAttach();
            }
        });
        et_msg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                goAttach();
            }
        });
        et_msg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && TextUtil.isNotEmpty(s.toString())) {
                    btn_add.setVisibility(View.GONE);
                    btn_send.setVisibility(View.VISIBLE);
                } else {
                    btn_add.setVisibility(View.VISIBLE);
                    btn_send.setVisibility(View.GONE);
                }
            }
        });


        rl_photo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIMinputListener != null)
                    mIMinputListener.onSelectPhoto();
            }
        });
        rl_take_photo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIMinputListener != null)
                    mIMinputListener.onTakePhoto();
            }
        });
        rl_doc.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIMinputListener != null)
                    mIMinputListener.onSelectDoc();
            }
        });

        btn_speak.registerIAudioRecorder(new AudioRecorderButton.IAudioRecorderListener() {
            @Override
            public void onSendAudio(String audioPath, int sends) {
                if (mIMinputListener != null)
                    mIMinputListener.onSendAudio(audioPath, sends);
            }

            @Override
            public void onRequestPermission() {

            }
        });
        btn_send.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String msgTxt = et_msg.getText().toString();
                if (TextUtil.isNotEmpty(msgTxt) && mIMinputListener != null) {
                    mIMinputListener.onSendTxtMsg(msgTxt);
                }
                et_msg.setText("");
            }
        });
        mLayoutTrade.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //showTradeView();
                if (mIMinputListener != null) {
                    mIMinputListener.onTrade(true);
                }
            }
        });
    }


    private void goAttach(){
        ll_attach_area.setVisibility(View.GONE);
        isShowAttach=false;
    }

    private void showTradeView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_trade, null);
        if (popupWinow == null)
            popupWinow = new YunShlPopupWinow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWinow.setCommonConfig((Activity) getContext(), YunShlPopupWinow.AnimaDirect.DIRECT_BOTTOM_TOP);
        TextView tv_sale = (TextView) view.findViewById(R.id.tv_sale);
        TextView tv_buy = (TextView) view.findViewById(R.id.tv_buy);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_sale.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIMinputListener != null) {
                    mIMinputListener.onTrade(false);
                }
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
            }
        });
        tv_buy.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIMinputListener != null) {
                    mIMinputListener.onTrade(true);
                }
                if (popupWinow != null) {
                    popupWinow.dismiss();
                }
            }
        });
        tv_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWinow.dismiss();
            }
        });

        popupWinow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    public void registerIMinputListener(IMinputListener mIMinputListener) {
        this.mIMinputListener = mIMinputListener;
    }

    public EditText getEt_msg() {
        return et_msg;
    }

    private void onInputSwitch(View view) {
        goAttach();
        isVoice = !isVoice;
        if (isVoice) {
            SoftInputUtil.hideSoftInput(getContext(), view);
            btn_speak.setVisibility(View.VISIBLE);
            btn_input_switch.setBackgroundResource(R.drawable.btn_message_keyboard);
            et_msg.setVisibility(View.GONE);
        } else {
            btn_speak.setVisibility(View.GONE);
            et_msg.setVisibility(View.VISIBLE);
            btn_input_switch.setBackgroundResource(R.mipmap.message_icon_chat_voice);
        }

    }

    private void onAdd() {

        isShowAttach = !isShowAttach;

        if (isShowAttach) {

            ll_attach_area.setVisibility(View.VISIBLE);

        } else {

            ll_attach_area.setVisibility(View.GONE);
        }
    }

    public void hideTradeView(boolean hide) {
       /* if (hide) {
            mLayoutTrade.setVisibility(GONE);
            mLineTrade.setVisibility(GONE);
        } else {
            if (Config.VERSION > 1) {
                mLayoutTrade.setVisibility(VISIBLE);
                mLineTrade.setVisibility(VISIBLE);
            }
        }*/
        mLayoutTrade.setVisibility(GONE);
        mLineTrade.setVisibility(GONE);
    }


    public void hideAttachArea() {
        if (ll_attach_area.getVisibility() == VISIBLE)
            ll_attach_area.setVisibility(View.GONE);
    }

}
